package io.renren.modules.sp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.sp.dao.TAreaDao;
import io.renren.modules.sp.dto.TAreaDTO;
import io.renren.modules.sp.entity.TAreaEntity;
import io.renren.modules.sp.service.TAreaService;
import io.renren.modules.sys.dto.ParamDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户地区
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2022-06-12
 */
@Service
public class TAreaServiceImpl extends CrudServiceImpl<TAreaDao, TAreaEntity, TAreaDTO> implements TAreaService {

    @Override
    public QueryWrapper<TAreaEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<TAreaEntity> wrapper = new QueryWrapper<>();
        String code = (String)params.get("code");
        String pcode = (String)params.get("pcode");
        String name = (String)params.get("name");
        String jiancheng = (String)params.get("jiancheng");
        String quhao = (String)params.get("quhao");

        wrapper.like(StringUtils.isNotBlank(code), "code", code);
        wrapper.like(StringUtils.isNotBlank(pcode), "pcode", pcode);
        wrapper.like(StringUtils.isNotBlank(name), "name", name);
        wrapper.like(StringUtils.isNotBlank(jiancheng), "jiancheng", jiancheng);
        wrapper.like(StringUtils.isNotBlank(quhao), "quhao", quhao);
        return wrapper;
    }

    /**
     * 查字典
     * */
    @Override
    public List<ParamDto> queryDict(String pCode) {
        QueryWrapper<TAreaEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pcode",pCode);
        return  baseDao.selectList(wrapper).stream().map(v->new ParamDto(v.getCode(),v.getName())).collect(Collectors.toList()) ;
    }

    /**
     * 根据编码查询地区
     * @param code
     * @return TAreaEntity
     * */
    public TAreaEntity getByCode(String code) {
        QueryWrapper<TAreaEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("code",code);
        List<TAreaEntity> list = baseDao.selectList(wrapper);
        TAreaEntity entity = list.size()>0?list.get(0):null;
        return  entity;
    }

    /**
     * 查询当前节点
     * @param code
     * @return
     */
    @Override
    public List<ParamDto> queryByCode(String code){
        QueryWrapper<TAreaEntity> wrapper = new QueryWrapper<>();
        wrapper.in("code", Arrays.asList(code.split(",")));
        return baseDao.selectList(wrapper).stream().map(v->new ParamDto(v.getCode(),v.getName())).collect(Collectors.toList()) ;
    }
}
